package models

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
)

var DB *gorm.DB

func Init() {
	var err error
	// 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
	dsn := "root:root@tcp(127.0.0.1:3306)/cart?charset=utf8mb4&parseTime=True&loc=Local"
	DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		panic("数据库连接失败")
	} else {
		log.Print("数据库连接成功")
	}
	err = DB.AutoMigrate(&User{}, &Cart{}, &Order{})
	if err != nil {
		panic("数据表迁移失败")
	} else {
		log.Print("数据表迁移成功")
	}
}
